package org.cx.oa.web.struts.actions;

import java.util.List;
import org.cx.oa.entity.Department;
import org.cx.oa.entity.Employee;
import org.cx.oa.entity.Post;
import org.cx.oa.entity.RightGroups;
import org.cx.oa.service.EmployeeService;
import org.cx.oa.util.BeanFactory;
import org.cx.oa.util.HibernateSessionUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List<Employee> employees;
	private int page = 1;
	private int sum = 0;
	private int sumPage = 0;
	private int pageSize = 5;
	private String select;
	private String condition;
	private Employee employee;
	private List<Department> departments;
	private List<Post> posts;
	private List<RightGroups> rightGroups;
	private int userId;
	
	private EmployeeService employeeService=BeanFactory.getImpl(EmployeeService.class);
	
	
	private void getAll()
	{		
		employeeService.setSelect(select);
		employeeService.setCondition(condition);
		employeeService.setPage(page);
		employees=employeeService.getByPageALL();
		sum=employeeService.getSum();
		sumPage=employeeService.getSumPage();
		
	}
	public String execute() {
		getAll();
		return SUCCESS;
	}
	
	public String fenye() {
		ActionContext ctx = ActionContext.getContext();
		select = (String) ctx.getSession().get("select");
		condition = (String) ctx.getSession().get("condition");
		return execute();
	}
	public String Add()
	{
//		Session session = HibernateSessionUtil.openSession();
//		session.beginTransaction();
//		session.save(employee);
//		session.getTransaction().commit();
//		HibernateSessionUtil.CloseSessionAndIfNull();
		
		employeeService.Add(employee);
		return "tolist";
	}
	public String getById()
	{
		employee= employeeService.getById(userId);
		return "byId";
	}
	public String update()
	{
//		Session session = HibernateSessionUtil.openSession();
//		session.beginTransaction();
//		session.update(employee);
//		session.getTransaction().commit();
//		HibernateSessionUtil.CloseSessionAndIfNull();
		employeeService.update(employee);
		return "tolist";
	}
	public String delete()
	{
//		Session session = HibernateSessionUtil.openSession();
//		session.beginTransaction();
//		session.delete(employee);
//		session.getTransaction().commit();
//		HibernateSessionUtil.CloseSessionAndIfNull();
		
		
		employeeService.delete(employee);
		return "tolist";
	}
	
	
	public int getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * @return the sum
	 */
	public int getSum() {
		return sum;
	}

	/**
	 * @param sum the sum to set
	 */
	public void setSum(int sum) {
		this.sum = sum;
	}

	/**
	 * @return the sumPage
	 */
	public int getSumPage() {
		return sumPage;
	}

	/**
	 * @param sumPage the sumPage to set
	 */
	public void setSumPage(int sumPage) {
		this.sumPage = sumPage;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the select
	 */
	public String getSelect() {
		return select;
	}

	/**
	 * @param select the select to set
	 */
	public void setSelect(String select) {
		this.select = select;
	}

	/**
	 * @return the condition
	 */
	public String getCondition() {
		return condition;
	}

	/**
	 * @param condition the condition to set
	 */
	public void setCondition(String condition) {
		this.condition = condition;
	}

	/**
	 * @return the employees
	 */
	public List<Employee> getEmployees() {
		return employees;
	}
	/**
	 * @return the employee
	 */
	public Employee getEmployee() {
		return employee;
	}
	/**
	 * @param employee the employee to set
	 */
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	/**
	 * @return the departments
	 */
	public List<Department> getDepartments() {
		Session session = HibernateSessionUtil.openSession();
		Criteria criteria=session.createCriteria(Department.class);
		departments=criteria.list();
		HibernateSessionUtil.CloseSessionAndIfNull();
		return departments;
	}
	
	public List<Post> getPosts() {
		Session session = HibernateSessionUtil.openSession();
		Criteria criteria=session.createCriteria(Post.class);
		posts=criteria.list();
		HibernateSessionUtil.CloseSessionAndIfNull();
		return posts;
	}
	public List<RightGroups> getRightGroups() {
		Session session = HibernateSessionUtil.openSession();
		Criteria criteria=session.createCriteria(RightGroups.class);
		rightGroups=criteria.list();
		HibernateSessionUtil.CloseSessionAndIfNull();
		return rightGroups;
	}
	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
}
